import java.util.HashMap;

/*
 * @lc app=leetcode.cn id=904 lang=java
 *
 * [904] 水果成篮
 */

// @lc code=start
class Solution {
    public int totalFruit(int[] fruits) {
        int left=0;
        int subL=0;
        int res=0;
        HashMap<Integer,Integer> basket=new HashMap<>();
        for(int right=0;right<fruits.length;right++){
            basket.put(fruits[right], basket.getOrDefault(fruits[right], 0)+1);
            while(basket.size()>2){//如何更新窗口左边坐标
                basket.put(fruits[left], basket.get(fruits[left])-1);
                if(basket.get(fruits[left])==0){
                    basket.remove(fruits[left]);
                }
                left++;
            }
            subL=right-left+1;
            res=Math.max(res, subL);
        }
        return res;
    }
}



// @lc code=end

